const Base = require('../../base.js');

module.exports = class extends Base {
  /// 获取任务列表
  async indexAction() {
    let data = this.get()
    let json = {}
    if (data.type == 0  ) json.reward = {$lte: 50} // 普通
    else if (data.type == 1) json.reward = {$lte: 100} // 白银
    else if (data.type == 2) json.reward = {$lte: 200} // 黄金
    else if (data.type == 3) json.reward = {$lte: 300} // 砖石

    let userInfo = await this.session('user_info')

    let result = await this.model('task/index').list(data.page, data.num, json)

    if (userInfo && result.length > 0) {
      let tidList = []
      result.forEach(item => tidList.push(item._id))
      let task_result = await this.model('task_result/index').list(1, data.num, {
        uid: userInfo._id,
        tid: {
          $in: tidList
        }
      })
      tidList = []
      task_result.result.forEach(item => tidList.push(item.tid))
      result.forEach((item, index) => {
        if (tidList.indexOf(result[index]._id.toString()) != -1) {
          result[index]._doc.receive = 1
        } else {
          result[index]._doc.receive = 0
        }
      })
    }
    // console.log(result)
    this.json({
      status: 200, message: 'success', data: result
    })
  }
};
